# Copyright 1999-2007 Sergey A. Sukiyazov <sukiazov@mail.ru>
# Distributed under the terms of the GNU General Public License v2
# $Header: Exp $

inherit eutils autotools depend.php

MY_HTTPDPATH=/var/www/localhost/htdocs

DESCRIPTION="SAMS is a WEB-based interface and tool to manage users of SQUID proxy."
HOMEPAGE="http://sams.perm.ru/"
SRC_URI="http://sams.perm.ru/download/sams-${PV}.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="x86"
IUSE=""

RDEPEND="net-proxy/squid
	dev-db/mysql
	media-libs/gd
	dev-libs/libpcre
	www-servers/apache"

DEPEND="${RDEPEND}"

need_php

pkg_setup() {
        require_php_with_use pcre mysql gd zlib
}


src_unpack() {
	unpack ${A}

	cd "${S}"
	epatch "${FILESDIR}/${P}-fix-Makefile.in.patch"
	eautoconf
}

src_compile() {
	econf --with-httpd-locations=${MY_HTTPDPATH} || die "configure has failed"
	make || die "make has failed"
}

src_install() {
	mkdir -p ${D}/{etc,etc/init.d}
	mkdir -p ${D}/${MY_HTTPDPATH}

	# Install SAMS
	make DESTDIR=${D} install || die "einstall failed"

	# remove tmp dir
	rm -fr ${D}/tmp

	# change init script for gentoo
	if [ -f ${D}/etc/init.d/samsd ] ; then
		rm -f ${D}/etc/init.d/samsd
	fi
	install -m 0755 ${FILESDIR}/samsdaemon.init ${D}/etc/init.d/samsdaemon

	insinto /usr/share/sams/data
	doins mysql/create_sams_user.sql #mysql/recover_admin_passwd.sql

	dodoc CHANGELOG COPYING INSTALL* README* VERSION          
}

pkg_postinst() {
        einfo "Execute the following command to setup the initial database environment:"
        einfo
        einfo "emerge --config =${CATEGORY}/${PF}"
        einfo
}

pkg_config() {
	einfo "Create MySQL database sams_db (enter password for MySQL root user)"
	mysql -u root -p  < /usr/share/sams/data/sams_db.sql || die "create database failed"
	einfo
	einfo "Create SQUID MySQL database (enter password for MySQL root user)" 
	mysql -u root -p < /usr/share/sams/data/squid_db.sql || die "create database failed"
	einfo
	einfo "Create SAMS user for MySQL (enter password for MySQL root user)" 
	mysql -u root -p < /usr/share/sams/data/create_sams_user.sql || die "create database failed"
	einfo
}
